#ifndef DATA_STRUCTURE_CTRLUTIL_H
#define DATA_STRUCTURE_CTRLUTIL_H

#include <iostream>
#include "src/HuffmanTree.h"

using namespace std;

class CtrlUtil {
private:
    HuffmanTree *huffmanTree = new HuffmanTree();//哈夫曼树封装类
    string sourceFile;//原文文件
    string encodeFile;//编码文件
    string compressionFile;//编码压缩文件
public:
    CtrlUtil *start() {
        while (true) {
            cout << "********************************************************************** \n"
                    "* 哈夫曼编码译码器 * \n"
                    "* 1、选择需要进行编码的文件 * \n"
                    "* 2、建立哈夫曼树 *\n"
                    "* 3、建立密码本并对文件编码 *\n"
                    "* 4、选择需要进行解码的文件并解码 *\n"
                    "* 5、按位压缩方式对文件进行压缩 *\n"
                    "* 6、解压并解码文件 *\n"
                    "* *\n"
                    "**********************************************************************\n";
            int i;
            cin >> i;
            switch (i) {
                case 1:
                    cout << "(1、选择需要进行编码的文件)" << endl;
                    q1();
                    break;
                case 2:
                    cout << "(2、建立哈夫曼树)" << endl;
                    q2();
                    break;
                case 3:
                    cout << "(3、建立密码本并对文件编码)" << endl;
                    q3();
                    break;
                case 4:
                    cout << "(4、选择需要进行解码的文件并解码)" << endl;
                    q4();
                    break;
                case 5:
                    cout << "(5、按位压缩方式对文件进行压缩)" << endl;
                    q5();
                    break;
                case 6:
                    cout << "(6、按位压缩方式对文件进行压缩)" << endl;
                    q6();
                    break;
                default:
                    return this;
            }
            cout << "-------------" << endl;
        }
    }

    void q1() {
        cout << "输入文件路径:" << endl;
        cin >> sourceFile;
        cout << "成功！" << endl;
    }

    void q2() {
        huffmanTree->createTree(sourceFile);
        cout << "成功！" << endl;
    }

    void q3() {
        cout << "输入编码输出文件路径:" << endl;
        cin >> encodeFile;
        huffmanTree->createCodeBook(R"(D:\code\data_structure\exp6\data\password_book.txt)");
        huffmanTree->encode(sourceFile, encodeFile);
        cout << "成功！" << endl;
    }

    void q4() {
        cout << "输入待解码文件路径:" << endl;
        string inFile;
        cin >> inFile;
        cout << "输入解码输出文件路径:" << endl;
        string outFile;
        cin >> outFile;
        huffmanTree->decodeF2F(inFile, outFile);
        cout << "成功！" << endl;
    }

    void q5() {
        cout << "输入压缩输出文件路径:" << endl;
        cin >> compressionFile;
        huffmanTree->compression(encodeFile, compressionFile);
        cout << "压缩比:" << (float) getFileSize(compressionFile) / (float) getFileSize(sourceFile) << endl;
    }

    void q6() {
        cout << "输入解压输出文件路径:" << endl;
        string outFile;
        cin >> outFile;
        huffmanTree->decompression(compressionFile, outFile);
        cout << "成功！" << endl;
    }

    static int getFileSize(string fileSrc) {
        FILE *file = fopen(fileSrc.c_str(), "r");
        fseek(file, 0, SEEK_END);
        int size = ftell(file);
        fclose(file);
        return size;
    }
};

#endif //DATA_STRUCTURE_CTRLUTIL_H
